//
//  CityChooseDataBase.m
//  itboye2015002
//
//  Created by boye_mac1 on 15/9/15.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "CityChooseDataBase.h"

#import "FMDatabase.h"

static FMDatabase *__db;
#define FILE_M [NSFileManager defaultManager]

@implementation CityChooseDataBase

//数据库初始化
+ (void)initDatabase{
    
    //把数据库文件从工程文件及中赋值到Documents中
    NSString *sandBoxPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/itboyeCityChoose.db"];
    if (![FILE_M fileExistsAtPath:sandBoxPath]) {
        NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"itboye" ofType:@"db"];
        [FILE_M copyItemAtPath:databasePath toPath:sandBoxPath error:nil];
    }
    
    if (!__db) {
        __db = [[FMDatabase alloc] initWithPath:sandBoxPath];
    }
}


//保护数据库安全
+(void) safeDataBase{
    
    [self initDatabase];
    
    if ([__db open]) {
        [__db close];
    }
}

// 城市选择器模型
+(NSArray *) getChooseCityList{

    NSMutableArray * provinceArr = [[NSMutableArray alloc] initWithCapacity:0];
    
   [provinceArr setArray: [self getAllProvinceModel]];
    return provinceArr;
}

// 获得省份
+(NSMutableArray *)getAllProvinceModel{
    
    [self safeDataBase];
    [__db open];
    
    NSMutableArray  * provinceArr = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSString * sql =  @"SELECT  * FROM common_province";
    
    FMResultSet * set = [__db executeQuery:sql];
    while ([set next]) {
        BoyeProvinceModel * provinceModel = [[BoyeProvinceModel alloc] init];

        provinceModel.comID = [set intForColumn:@"id"];
        provinceModel.provinceID = [set stringForColumn:@"provinceID"];
        provinceModel.province = [set stringForColumn:@"province"];
        provinceModel.countryid = [set intForColumn:@"countryid"];
        
        [provinceArr addObject:provinceModel];
    }
    [set close];
    [__db close];
    
    return provinceArr;
}

// 获得城市

+(NSMutableArray *) getAllCityProvinceID:(NSString *)provinceID{

    [self safeDataBase];
    [__db open];
    
    NSMutableArray  * cityArr = [[NSMutableArray alloc] initWithCapacity:0];

    NSString * sql = [NSString stringWithFormat:@"SELECT * FROM common_city WHERE father = %@",provinceID];
    
    FMResultSet * set = [__db executeQuery:sql];
    while ([set next]) {
        BoyeCityModel * cityModel = [[BoyeCityModel alloc] init];
        cityModel.comID = [set intForColumn:@"id"];
        cityModel.cityID = [set stringForColumn:@"cityID"];
        cityModel.city = [set stringForColumn:@"city"];
        cityModel.father = [set stringForColumn:@"father"];
        [cityArr addObject:cityModel];
    }
//    DLog(@"   - %@",cityArr);
    [set close];
    [__db close];
    return cityArr;
}

// 获得地区

+(NSMutableArray *) getAllArearCityID:(NSString *)cityID{
    
    [self safeDataBase];
    [__db open];
    
    NSMutableArray  * areaArr = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSString * sql = [NSString stringWithFormat:@"SELECT * FROM common_area WHERE father = %@",cityID];
    
    FMResultSet * set = [__db executeQuery:sql];
    while ([set next]) {
        BoyeAreaModel * areaModel = [[BoyeAreaModel alloc] init];
        areaModel.comID = [set intForColumn:@"id"];
        areaModel.areaID = [set stringForColumn:@"areaID"];
        areaModel.area = [set stringForColumn:@"area"];
        areaModel.father = [set stringForColumn:@"father"];
        
        [areaArr addObject:areaModel];
    }
    [set close];
    [__db close];
    
    return areaArr;
}

@end
